<?php

	include "../database.php";
	
	$user_id = $_POST["user_id"];
	$username = $_POST["username"];
	$name = $_POST["name"];
	$last_name = $_POST["last_name"];
	$mail = $_POST["mail"];
	$pass = $_POST["password"];
	$pass_confirm = $_POST["password_confirm"];
	$admin = $_POST["admin"];
	$success = 0;
//	$data = array('pass' => $pass, 'pass_confirm' => $pass_confirm);
//	echo json_encode($data);
//	$data = array('user_id' => $user_id, 'username' => $sameUsernameResult[1], 'name' => $sameUsernameResult[2], 'last_name' => $sameUsernameResult[3], 'mail' => $sameUsernameResult[4], 'pass' => $sameUsernameResult[5], 'pass_confirm' => $pass_confirm, 'admin' => $sameUsernameResult[6], 'id_msg' => $id_msg, 'success' => $success );
//	echo json_encode($data);
	
	// Poizvedba ce ima ta username drugi uporabnik
		$checkUsernameQuery = "SELECT id,username FROM user WHERE username='$username';";
    	$checkUsernameResult = executeQuery($checkUsernameQuery);
    	
    	$rowsNumber = mysql_num_rows($checkUsernameResult);
    	$checkUsernameResult = mysql_fetch_row($checkUsernameResult);
   		
   		// Poizvedba da dobimo nas trenutni username
   		$sameUsernameQuery = "SELECT * FROM user WHERE id=$user_id;";
   		$sameUsernameResult = executeQuery($sameUsernameQuery);
   		
   		$rowsNumber2 = mysql_num_rows($sameUsernameResult);
   		$sameUsernameResult = mysql_fetch_row($sameUsernameResult);
    	    	
    	// Poizvedba za spreminjanje podatkov
    	$query = "UPDATE user SET username='$username', name='$name', last_name='$last_name', mail='$mail', pass='$pass', admin='$admin' WHERE id=$user_id;";
	
	//	Preverjamo enakost vnesenih gesel 
	if ( ( $pass == $pass_confirm)) {
		
		if (($pass == "" && $pass_confirm == "")){
			$query = "UPDATE user SET username='$username', name='$name', last_name='$last_name', mail='$mail', pass='$sameUsernameResult[5]', admin='$admin' WHERE id=$user_id;";
		}
						
	//    	 Ce spreminjamo ostale podatke, username pa ostane enak
			if ($sameUsernameResult[1] == $username ) {  
			
				
			
				executeQuery($query);
				$id_msg = message(200);
				$data = array('id_msg' => "$id_msg", 'success' => '1', 'username' => "$username", 'mail' => "$mail");	
				echo json_encode($data);
		
			// Ce username se ni zaseden
			} elseif($rowsNumber < 1){
	
				executeQuery($query);
				$id_msg = message(200);
				$data = array('id_msg' => "$id_msg", 'success' => '1', 'username' => "$username", 'mail' => "$mail");	
				echo json_encode($data);
				
			// Drugace sporoci, da je username zaseden
			} else {
				$id_msg = message(101);			
				$data = array('id_msg' => "$id_msg", 'success' => '0', 'username' => "$sameUsernameResult[1]", 'mail' => "$sameUsernameResult[4]");	
				echo json_encode($data);
			}
							
	// Drugace sporoci napako za geslo	
	} else {
		$id_msg = message(102);
		$data = array('id_msg' => "$id_msg", 'success' => '0', 'username' => "$sameUsernameResult[1]", 'mail' => "$sameUsernameResult[4]");	
		echo json_encode($data);
//		$success = 0;
	}

?>
